package com.ziq.zcamp.post

import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.amap.api.services.core.LatLonPoint
import com.amap.api.services.core.PoiItem
import com.amap.api.services.poisearch.PoiResult
import com.amap.api.services.poisearch.PoiSearch
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.zcamp.util.MapUtil
import com.ziq.zcamp.view.StatusView
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class SelectLocationPoiFragmentViewModel @Inject constructor() : ViewModel() {


    val statusShow = MutableLiveData<Int>()
    val listRequestFinished = MutableLiveData<Boolean>()
    val listHasMore = MutableLiveData<Boolean>()
    val list = MutableLiveData<MutableList<PoiItem>>()

    private var page = 1
    private var pageSize = 20
    var keyword: String? = null
    var myLatLng: LatLonPoint? = null

    fun onRefresh(showProgress: Boolean = false){
        page = 1
        if(showProgress){
            statusShow.postValue(StatusView.LOADING)
        }
        requestList()
    }

    fun onLoadMore(){
        requestList()
    }

    fun requestList() {
        val queryTxt = keyword?:""
        val query = PoiSearch.Query(queryTxt, "", "")
        val isNear: Boolean = queryTxt.isEmpty()
        query.pageNum = page
        query.pageSize = pageSize
        //V7.6.0之前都是默认的EXTENSIONS_BASE,新版本中需要返回所有信息设置为 EXTENSIONS_ALL
        query.extensions = PoiSearch.EXTENSIONS_ALL

        MapUtil.poiSearch(query = query,myLatLng = if(isNear) myLatLng else null, callback = object : PoiSearch.OnPoiSearchListener {
            override fun onPoiItemSearched(poiItem: PoiItem?, p1: Int) {
                listRequestFinished.postValue(true)
            }

            override fun onPoiSearched(result: PoiResult?, p1: Int) {
                if(result != null){
                    val tempList = mutableListOf<PoiItem>()
                    if(page != 1 && list.value != null){
                        tempList.addAll(list.value!!)
                    }
                    if(result.pois.isNotEmpty()){
                        tempList.addAll(result.pois)
                        page++
                        listHasMore.postValue(true)
                    } else {
                        listHasMore.postValue(false)
                    }
                    list.postValue(tempList)

                    if(tempList.isNotEmpty()){
                        statusShow.postValue(StatusView.IDLE)
                    }else {
                        statusShow.postValue(StatusView.NO_DATA)
                    }
                } else {
                    if(page == 1){
                        statusShow.postValue(StatusView.NET_ERROR)
                    } else {
                        statusShow.postValue(StatusView.IDLE)
                    }
                    listHasMore.postValue(false)
                }
                listRequestFinished.postValue(true)
            }
        }, onFail = {
            Toast.makeText(GlobalContextManager.appContext!!, "需同意隐私协议！", Toast.LENGTH_SHORT).show()
            listRequestFinished.postValue(true)
        })
    }

}